package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo5 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        /*
        Person p = new Person("张三");
        p.say("大家好");
        p.say("大家好",5);
         */
        Class cls = Class.forName("reflect.Person");
        Constructor c = cls.getConstructor(String.class);
        Object obj = c.newInstance("张三");//Person obj = new Person("张三");

        //say(String)
        Method method = cls.getMethod("say",String.class);
        method.invoke(obj,"大家好!");//obj.say("大家好!");

        //say(String,int)
        Method method1 = cls.getMethod("say",String.class,int.class);
        method1.invoke(obj,"大家好!",5);//obj.say("大家好!",5);
    }
}
